package com.xkdx.guangguang.card;

import java.util.HashMap;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.xkdx.caipiao.R;
import com.xkdx.guangguang.adapter.CardDetailAdapter;
import com.xkdx.guangguang.fragment.BaseFragment;
import com.xkdx.guangguang.module.network.AbsModule;
import com.xkdx.guangguang.module.statics.IConstants;

public class CardDetailFragment extends BaseFragment {

	View view;

	CardDetailAction action;
	CardDetailModule module;
	CardDetailPresistence presistence;

	ListView lv;
	ImageView imageView;
	TextView detail, time;
	ImageLoader imageLoader = ImageLoader.getInstance();
	DisplayImageOptions options;
	Button back;
	CardDetailAdapter adapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.focuse_brand_discount)
				.showImageForEmptyUri(R.drawable.focuse_brand_discount)
				.showImageOnFail(R.drawable.focuse_brand_discount)
				.resetViewBeforeLoading().cacheOnDisc()
				.imageScaleType(ImageScaleType.EXACTLY)
				.bitmapConfig(Bitmap.Config.RGB_565).build();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.card_detail, container, false);
		getCardDetail();
		findView();
		setOnClick();

		return view;
	}

	private void getCardDetail() {
		action = new CardDetailAction(getArguments().getString("CinfoID"),
				String.valueOf(IConstants.lat), String.valueOf(IConstants.lon),
				"1", "10");
		module = new CardDetailModule();
		presistence = new CardDetailPresistence(this);
		presistence.setActitons(action);
		presistence.setModule(module);
		presistence.execute();

	}

	@Override
	public void findView() {
		imageView = (ImageView) view.findViewById(R.id.card_im);
		detail = (TextView) view.findViewById(R.id.card_detail);
		time = (TextView) view.findViewById(R.id.card_time);
		back = (Button) view.findViewById(R.id.back_card_detail);
		lv = (ListView) view.findViewById(R.id.card_detail_ls);

	}

	@Override
	public void setOnClick() {
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				getFragmentManager().popBackStack();

			}
		});
	}

	@Override
	public void showLoading() {
		// TODO Auto-generated method stub

	}

	@Override
	public void showLoaded(HashMap<String, AbsModule> map) {
		dataLoadDialog.dismiss();
		if (map.containsKey("0")) {
			setMessage();
		} else {
			refresh(map);
		}

	}

	private void setMessage() {
		imageLoader.displayImage(module.cardDetail.getPicture(), imageView,
				options);
		detail.setText(module.cardDetail.getRegular());
		time.setText(module.cardDetail.getStartTime().substring(0, 10) + "到"
				+ module.cardDetail.getEndTime().subSequence(0, 10));
		if (module.list.size() != 0) {
			adapter = new CardDetailAdapter(getActivity(), module.list);
			lv.setAdapter(adapter);
		}

	}

	@Override
	public void showClick(HashMap<String, AbsModule> map) {
		// TODO Auto-generated method stub

	}

	@Override
	public void showLoadMore(HashMap<String, AbsModule> map) {
		// TODO Auto-generated method stub

	}

}
